//
//  RHQuestionVC.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/10/30.
//

#import "RHQuestionVC.h"
#import "RHQuestionCell.h"
#import "RHQuestionModel.h"
#import "YYLabel.h"
#import "NSAttributedString+YYText.h"
#import "RHBaseWebViewController.h"

@interface RHQuestionVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataArr;

@end

@implementation RHQuestionVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"常见问题".rh_localized;
    self.view.backgroundColor = CFTabViewBgColor;
    [self setupUI];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)setupUI{
    
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"server_help_file".rh_localized ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    NSArray *arr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    self.dataArr = [[NSArray yy_modelArrayWithClass:[RHQuestionModel class] json:arr] copy];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [_tableView setBackgroundColor:UIColor.whiteColor];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    [_tableView registerClass:[RHQuestionCell class] forCellReuseIdentifier:@"RHQuestionCell"];
    _tableView.tableFooterView = [self footerView];
    [self.view addSubview:_tableView];
    [_tableView rounded:8 width:1 color:kBorderColor];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(15, 15, kSafe_bottom>0?kSafe_bottom:15,15));
        
    }];
    
    if (@available(iOS 11.0, *)) {
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    if (@available(iOS 15.0, *)) {
        _tableView.sectionHeaderTopPadding = 0;
    }
}

-(UIView *)footerView
{
    YYLabel *contentL = [[YYLabel alloc] init];
    NSString *str = @"有疑问？请联系在线客服>>".rh_localized;
    //设置多行
    contentL.numberOfLines = 0;
    //这个属性必须设置，多行才有效
    contentL.preferredMaxLayoutWidth = SCREEN_WIDTH-30;
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:str];
    attri.yy_lineSpacing = 8;
    attri.yy_alignment = NSTextAlignmentCenter;
    attri.yy_font = [UIFont systemFontOfSize:15];
    //FontColor(@"#3ECE80")
    attri.yy_color = [UIColor colorWithHexString:@"#666666"];
    NSRange range1 = [str rangeOfString:@"请联系在线客服>>".rh_localized];

    [attri yy_setTextHighlightRange:range1 color:CMainColorGreen backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        RHBaseWebViewController *webVC = [[RHBaseWebViewController alloc]init];
        webVC.navTitle = @"智能客服".rh_localized;
        webVC.urlStr = @"https://www.ruihenergy.com";
        [self.navigationController pushViewController:webVC animated:YES];
        
    }];
    
    //用label的attributedText属性来使用富文本
    contentL.attributedText = attri;
    CGSize maxSize = CGSizeMake(SCREEN_WIDTH-30, MAXFLOAT);
    //计算文本尺寸
    YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:maxSize text:attri];
    contentL.textLayout = layout;
    CGFloat introHeight = layout.textBoundingSize.height;
    
    
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kUI_WINDOW_W, 80)];
    [footerView addSubview:contentL];
    [contentL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.mas_offset(25);
        make.right.mas_offset(-15);
        make.height.mas_equalTo(introHeight+1);
    }];
    
    return footerView;
}

#pragma mark - UITableViewDelegate,UITableViewDataSource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArr.count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    RHQuestionModel *model = self.dataArr[section];
    if(model.isOpen){
        return 1;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    RHQuestionModel *model = self.dataArr[section];
    CGFloat text_height = [[model.question rh_localized] getStringHeightFont:kSYSTEMFONT(15) limitWidth:kUI_WINDOW_W-30-15-35];
    
    return  15 + text_height + 15;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    RHQuestionModel *model = self.dataArr[section];
    CGFloat text_height = [[model.question rh_localized] getStringHeightFont:kSYSTEMFONT(15) limitWidth:kUI_WINDOW_W-30-15-35];
    // = 15 + 10 + 文字高度 +10
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kUI_WINDOW_W-30, 15 + text_height + 15)];
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kUI_WINDOW_W-30, 15 + text_height + 15)];
   
    [headerView addSubview:bgView];
    bgView.backgroundColor = UIColor.whiteColor;
    CGFloat titleWidth = bgView.width - 15 - 35;
    QMUILabel *titleLab = [[QMUILabel alloc]initWithFrame:CGRectMake(15, 15, titleWidth, text_height)];
    titleLab.numberOfLines = 0;
    titleLab.text = [model.question rh_localized];
    titleLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:01];
    titleLab.font = [UIFont systemFontOfSize:15];
    [bgView addSubview:titleLab];
    
    UIImageView *arrow = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"kwt_home_arrow"]];
    [bgView addSubview:arrow];
    [arrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(15);
        make.right.mas_offset(-10);
        make.centerY.equalTo(titleLab);
    }];
    
    QMUIButton *rightBtn = [[QMUIButton alloc]initWithFrame:bgView.bounds];
    [[rightBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        model.isOpen = !model.isOpen;
        
        [tableView reloadData];
    }];
    CGAffineTransform transform;
    if(model.isOpen){
        transform = CGAffineTransformRotate(CGAffineTransformIdentity, M_PI_2);
    }else{
        transform = CGAffineTransformIdentity;
    }
    [UIView animateWithDuration:0.3 animations:^{
        arrow.transform = transform;
    }];
    [bgView addSubview:rightBtn];
    
    if (!model.isOpen) {
        UIView *line = kView;
        line.backgroundColor = CFCellLineColor;
        [bgView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(15);
            make.right.mas_offset(-15);
            make.height.offset(1);
            make.bottom.mas_offset(0);
        }];
    }
   
    return headerView;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kUI_WINDOW_W, 8)];
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(15, 0, kUI_WINDOW_W-30, 8)];
    bgView.backgroundColor = UIColor.whiteColor;
    [bgView setBottomBorder];
    [footerView addSubview:bgView];
    return footerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RHQuestionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RHQuestionCell"];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    RHQuestionModel *model = self.dataArr[indexPath.section];
    cell.contentLab.text = [model.answer rh_localized];
    return cell;
}


@end
